"FIND, PROCESS AND FIND NEXT" LOOPS.

Here is a construct I have seen used many times since VEE's way of
dealing with these loops changed with VEE 4 ( or was it VEE 5? ).  It's
a useful construct to know anyway and so this note is designed as a
mini-tutorial for the topic.  Comments, ideas for improvements etc are
very welcome - please use the Questions/Answers/Comments form on the
website - www.PreciselySo.Co.UK

DISCLAIMER, WARRANTY AND COPYRIGHT
These examples are provided on an 'as is' basis with no explicit or
implied warranty.  Copyright is retained by the author but free use
including copying and use for business purposes is expressly permitted.

This explanation goes with a set of VEE files that you should also have
- they are in VEE 5 for widest appeal.

The Basics
==========
If you are familiar with VEE loops you can skip this bit - if not here
is how the basic VEE loop works.

From running training courses I find this topic always gives the
greatest problems.  VEE is sometimes described as being a 'flowchart'
language, that is, you draw the flowchart and it works!  It's not a bad
description but for the fact that most people draw flowchart loops as,
well, loops.  VEE takes what might be called a more fundamental view:
'DO' a process a set number of times, or until a condition is true.
This is shown in VEE as a 'stub' or siding ( a train analogy?? ) into
which the program goes until it can carry on [Loop 1.VEE].

Simple huh?  Okay there are a couple of extra objects we should know how
to use under the Flow -> Repeat menu: the Next and Break objects.  This
loop worked fine without them and so, unlike what may be a more familiar
text-based construct of FOR-NEXT is not required for simple count loops.
Talking of which, the For Count and For Range are fundamentally the
same.  The For Log Range is too but with a neat twist that you do not
have to compute the points if you are doing a log sweep.

Simply, Break stops the loop running and passes control/flow to the FOR
object's sequence out pin.  Next stops the current thread and asks for
the next iteration [Loop 2 - turning on the debug 'show flow' may help
to see what is happening here].  I admit it is not the most exciting
example.  Try deleting the Next object and see what happens to that
loop.

Loop 2 also shows 'sequential loops' - two separate loops, one running
after the other has finished.  The other scenario is that the loops are
nested which crops up many times e.g. FOR each page in the document FOR
each line FOR each character ( do something ).  I was going to show an
example of filling a 2D array with loops but typically I try to avoid
using loops for arrays - VEE is very good at handling arrays ( and data
in general ) so I have gone for moving a panel around the screen instead
[Loop 3].

Note that I could have used a For Range object for the vertical
step with an increment of 10 which would do the same thing but would be
more readable/understandable than having an unexplained *10 in the
ShowPanel formula box.

Try stopping the scan of each line half way without changing the numbers
in the FOR objects.

Try stopping the scan after the 4th line ( remember For Count starts at
zero ).

The last basic construct you should know is that using the Data ->
Collector object.  So often you make a series of measurements and then
want to process or store them.  As I said before, VEE handles arrays
very nicely and so 'collecting' the values into an array is a neat thing
to do.  Here is the basic construct to use the collector [Loop 4].

The collector is an anomaly for VEE devices because it has a green XEQ
pin ( stands for execute ).  It stores data in an array and squirts it
out when the XEQ pin is pinged.

A simple construct but one I find new users have problems with.  The
button in the middle of the Collector object allows you to collect 1D
arrays into bigger 1D arrays but the normal use is "n+1 Dim Array" i.e.
if you collect 1D arrays together you obtain 2D arrays [Loop 5].


Iterative Loops and Initial Values
==================================
There are times where you want to process some data and then, depending
on the result of that process, reprocess the data.  First you need to
decide the process, what the initial conditions are and what stops the
loop.

For example I want to guess your age.  I will give you a number and you
have to tell me 'higher' or 'lower'.

The process I choose to use is a binary search and I will choose a
maximum age of 127.  If you are older than that why are you playing with
VEE when you should be out playing golf?

I will start at 64.

I will end when you tell me I've guessed correctly.

Almost always we use an Until Break ( keep going til there's an answer,
or you've run out of whatever you were looking at ), a Junction to
provide the initial value and some form of conditional with a Break to
stop the loop [Interate 1.VEE].

There are actually two numbers going around the loop ( as an array )
which is actually fairly common - in this case my latest guess and also
the increment.  I could have some error checking that the increment does
not go below 1.

Just to explain this further, if needed, the loop iterator is the Until
Break - VEE won't let you run this sort of 'feedback loop' as it calls
it unless there is an iterator ( try deleting it and you'll see that
pressing the Help button in the error message is actually of use! ).

It also won't let you run this sort of loop without a juction feeding in
the default value(s) and the next guess.  Often the initial value is
zero or a blank string.

The Guess age user object is whatever process you want to apply and then
the output from this is normally tested to see if the loop conditions
have been met.
________________________________________________________________________

And finally, what prompted me to write this.  A popular thing to want to
do is to find something in a string, note where it is, or what comes
after it, and then look for the next occurrence.  For example I want to
look for the word "Start: " and then read the real number which follows
and place these numbers into an array. Have a look at the FindStart.txt
file.

The process is simple: look for the keyword and note where it starts,
read the next token ( a real number ) and throw away the bit of text we
have just examined.

Start point -  complete line of text

Stop point - when the result of strPosStr is negative i.e. no more
occurences of "Start: " can be found.  If it helps, here are the stages
I used to build this solution.

First retrieve the text from the file and pass into a strPosStr Formula
to look for "Start: " [Iterate 2a].

Now I know that the value I want starts at 53 + 7 characters and also my
next search wants to start from here or maybe one character later as we
know that there will be at least one digit.  In the same formula box,
then, I compute the remaining string and also extract the value of the
number I wish to find.  I happen to know that if you pass VEE a string
it will pick off the first number it finds and convert it to real if
required [Iterate 2b].

That's the hard work done!  Note that you don't have to use all of the
output pins as I have done - you can use them for temporary storage of
values computed in the formula box.

All that is needed now are the elements mentioned before:

a loop iterator

a junction for the default value

a loop condition test

[Iterate 2c] note I had to check for Start being -1 in the third line of
the formula box because the loop break is after the attempt to convert no
numbers to a real.

Finally this is tidied up and a data collector used to put the reals
into an array [Iterate 2d].

There may be some extras you'd want to put in for error handling e.g.
the keyword does not appear at all in the file.


Michael J. Watts Consultant Apr 2001
